﻿using System;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using OpenWaves.Reflection;

namespace OpenWaves.Testing.CodeAnalysisRules
{
    public class DefaultImplementationShouldPointToTypeImplementingInterface : ICodeAnalysisRule
    {
        public void Run(Assembly assembly)
        {
            var serviceInterfaces = assembly.GetTypes()
                .Where(t => t.GetCustomAttributes<DefaultImplementationAttribute>().Any());

            foreach (var serviceInterface in serviceInterfaces)
            {
                var defaultImplementationAttribute = serviceInterface.GetCustomAttributes<DefaultImplementationAttribute>().Single();
                
                Assert.IsTrue(serviceInterface.IsAssignableFrom(defaultImplementationAttribute.ImplementationType), "{0} does not implement {1} and is used as its default implementation.", defaultImplementationAttribute.ImplementationType, serviceInterface);
            }
        }         
    }
}